<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Calendar"%>
<%@page import="java.util.Date"%>
<%@ page import="java.net.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<%
   String username = "" ;
   String password = "" ;
   Cookie cookie = null;
   Cookie[] cookies = null;
   // 获取cookies的数据,是一个数组
   cookies = request.getCookies();
   if( cookies != null ){
      out.println("<h2> 查找 Cookie 名与值</h2>");
      for (int i = 0; i < cookies.length; i++){
         cookie = cookies[i];
        
         out.print("1参数名 : " + cookie.getName());
         out.print("<br>");
         out.print("2参数值: " + URLDecoder.decode(cookie.getValue(), "utf-8") +" <br>");
         out.print("<br>");
         //增加秒数 
         Date curr = new Date();
         Calendar calendar = Calendar.getInstance();    
         calendar.setTime(curr);
         //用当前时间，加上cookie过期的秒数
         calendar.add(Calendar.SECOND, cookie.getMaxAge());  
         curr = calendar.getTime();
         out.print("3过期时间: " + curr +" <br>");
         //格式化后显示
         SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS ");
         String currStr = formatter.format(curr);

         out.print("3过期时间: " + currStr +" <br>");
         
         out.print("4获取cookie的域名: " + cookie.getDomain() +" <br>");
         out.print("<br>");
         out.print("5获取cookie的路径: " + cookie.getPath() +" <br>");
         out.print("<br>");
         out.print("6描述cookie目的的注释: " + cookie.getComment() +" <br>");
         out.print("<br>");
         out.print("7getVersion: " + cookie.getVersion() +" <br>");
         out.print("<br>");
         out.print("------------------------------------<br>");

         
         if("username".equals(cookie.getName())){
        	 username = URLDecoder.decode(cookie.getValue(), "utf-8") ;
         }
         if("password".equals(cookie.getName())){
        	 password = URLDecoder.decode(cookie.getValue(), "utf-8") ;
         }
      }
  }else{
      out.println("<h2>没有发现 Cookie</h2>");
  }
%>
<script type="text/javascript">
//设置cookie，cookie有效期时间未GMT时间（距1970年1月1日临时的毫秒） 
//例如可以设置setCookie("password","12345",(3600000*24*180)),180有效 
function setCookie (name, value, expires) {  
	var expdate = new Date(); 
	expdate.setTime(expdate.getTime() + expires); 
	document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/"; 
} 
alert(document.cookie);
//根据cookie名，取得cookie值 
function getCookie(name) {   
	var search;  
	search = name + "=" 
	offset = document.cookie.indexOf(search) 
	if (offset != -1) { 
		offset += search.length ; 
		end = document.cookie.indexOf(";", offset) ; 
		if (end == -1) 
			end = document.cookie.length; 
		return unescape(document.cookie.substring(offset, end)); 
	}else{
		return "";
	}
} 

//删除某一cookie 
function deleteCookie(name) {    
	var expdate = new Date(); 
	expdate.setTime(expdate.getTime() - (3600 *24* 1000 * 1)); 
	setCookie(name, "", expdate); 
} 

//检查是否存在此cookie 
function checkCookie(cookieName,cookieValue){ 
	if (getCookie(cookieName)==cookieValue){ 
		return true; 
	}else{ 
		return false; 
	} 
}
</script>
</head>
<body>
	<h6>${error }</h6>
	<form action="<%=path%>/login" method="post">
	用户名 : <input type="text" name="username" value="<%=username%>" >
	密码 : <input type="password" name="password" value="<%=password%>" >
	<input type="submit" value="登录">
	</form>
</body>
</html>